// pages/vipCore/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
var last_scroll_position = 0
var new_scroll_position = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultAvatarUrl: defaultAvatarUrl,
    rootDocment: rootDocment,
    show: false,
    kefu: wx.getStorageSync('kefu') ? wx.getStorageSync('kefu') : '',
    is_write_style: true,
    index: 0,
    is_shop: wx.getStorageSync('is_shop'),
    meIsShow: true,
    num: 4,
    cartList: [],
    pay_num: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideTabBar();
    app.editTabbar();
    // this.getUserInfo()

  },
  to_purchase_index() {
    wx.navigateTo({
      url: '/pages/cart/purchase_index',
    })
  },
  //获取购物车列表
  setCartData: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    paraArr['type'] = 1;
    console.log(paraArr, 'paraArr')
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_cart',
      data: {
        user_id: paraArr['user_id'],
        type: 1,
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        let arr = res.data

        let num = 0
        arr.forEach((item, index) => {
          num += Number(item.hhr_price)
        })
        console.log('购物车', num);
        that.setData({
          cartList: arr,
          pay_num: num,
          hiddenLoading: true
        }, () => {

        });

      }
    })
  },
  scroll_view() {
    var that = this;
    let num = that.data.size
    num += 6
    that.setData({
      num: num
    })
  },
  onPageScroll: function (e) {
    // Do something when page scroll
    // console.log('我动了吗')
    // console.log(e, '滑动');
    var that = this
    let write_style = that.data.write_style
    last_scroll_position = e.scrollTop;
    if (new_scroll_position < last_scroll_position && last_scroll_position > 50) {
      that.setData({
        write_style: 'slideUp'
      })

    } else {
      that.setData({
        write_style: 'slideDown'
      })
    }
    new_scroll_position = last_scroll_position;
    let floatingFrame = this.selectComponent('#floatingFrame'); // 页面获取自定义组件实例
    floatingFrame.isHide(); // 通过实例调用组件事件
    if (that.data.write_style != write_style && that.data.write_style == 'slideDown') {
      // console.log('我动了吗')
      that.setData({
        is_write_style: true
      })


    }
    if (that.data.write_style != write_style && that.data.write_style == 'slideUp') {
      that.setData({
        is_write_style: false
      })


    }
  },
  swiperC(e) {
    console.log(e.detail.current);
    this.setData({
      index: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 获取用户信息
   */
  getUserInfo: function () {
    console.log('show')
    var that = this;
    var paraArr = new Array();
    paraArr['id'] = that.data.userInfo.userID;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_user/' + paraArr['id'],
      data: {
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res.data, '1111111111');
        that.setData({
          nickname: res.data.data.nickname,
          avatar: res.data.data.avatar,
          is_shop: res.data.data.is_shop,
          level: res.data.data.level_id,
          user_id: res.data.data.user_id,
          is_vip: res.data.data.is_vip,
          hiddenLoading: true,
          vip_express_time: res.data.data.vip_express_time
        });
        wx.setStorageSync('is_vip', res.data.data.is_vip)
        wx.setStorageSync('owner_shop_name', res.data.data.owner_shop_name)
        if (!res.data.data.avatar || !res.data.data.nickname) {
          console.log('???')
          that.setData({
            meIsShow: false
          })
          wx.setStorageSync('meIsShow', that.data.meIsShow)
        } else {
          console.log('???11')
          that.setData({
            meIsShow: true
          })
          wx.setStorageSync('meIsShow', true)
        }
      }
    })
  },
  onClickShow() {
    this.setData({
      show: true
    });
  },

  onClickHide() {
    console.log('9999')
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    let that = this
    // let meIsShow = wx.getStorageSync('meIsShow')
    let kefu = wx.getStorageSync('kefu')
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo,
        // meIsShow: meIsShow,
        kefu: kefu
      });
      that.getUserInfo();
      that.setCartData()
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    var that = this;
    return {
      title: '榴皇树',
      path: '/pages/index/index?scene=' + wx.getStorageSync('user_id3')
    }
  },

})